function getSelection(input) {
	
	if(!opener || opener == null || opener == self) {
		doc = document;
	}else{
		doc = opener.document;
	}
	
	if(typeof doc.selection != 'undefined')  { 
	    var range = doc.selection.createRange(); 
	    return range.text;
	} else if(typeof input.selectionStart != 'undefined')  { 
	    var start = input.selectionStart; 
	    var end = input.selectionEnd; 
	    return input.value.substring(start, end); 
	}else{ 
	    return "";
	}
	return selection
}

function insertText(input,text) {
	if(!opener || opener == null || opener == self) {
		doc = document;
	}else{
		doc = opener.document;
	}
	input.focus();
	if(typeof doc.selection != 'undefined') 
	{ 
	    var range = doc.selection.createRange(); 
	    range.text=""; 
		/* Einf¸gen des Formatierungscodes */
		range.text = text;
		/* Anpassen der Cursorposition */
		range = doc.selection.createRange();
		if (text.length == 0) {
			range.move('character', -text.length);
		} else {
			range.moveStart('character', text.length);      
		}
		range.select();

	    //if(text != null) input.value += text; 
	} 
	else if(typeof input.selectionStart != 'undefined') 
	{ 
	    var start = input.selectionStart; 
	    var end = input.selectionEnd; 
	    if(text != null) input.value = input.value.substr(0, start) + text + input.value.substr(end);    
	}else 
	{ 
	    if(text != null) input.value += text; 
	} 
}
/*
 *BBCode
*/
function filterBBCode(form,atag,etag,ausrichtung) {
	var seite;
	var input = document.formular.text; 
	
	if(ausrichtung=="ausrichtung")
	{
		var newIndex = form.ausrichtung.selectedIndex; 
		seite = form.ausrichtung.options[ newIndex ].value; 

	}
	if(ausrichtung=="size")
	{
		var newIndex = form.size.selectedIndex; 
		seite = form.size.options[ newIndex ].value;
	}
	if(ausrichtung=="color")
	{
		var newIndex = form.color.selectedIndex; 
		seite = form.color.options[ newIndex ].value; 
	}
	
	var sel = getSelection(input);
    
    if(sel != "") {
    	// Has selection
    	insertText(input,atag + seite+ ']' + sel +etag);
    }else{
    	// No Selection
    	var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:"); 
	    if(insText != null) insertText(input,atag + seite+ ']' + insText +etag); 
    }
    input.focus(); 
}

function bbcode_normal(form11,atag, etag) 
{ 
   
   	if(!opener || opener == null || opener == self)
	{
		var input = document.formular.text;
	}else{
	 	var input = opener.document.formular.text;
	}

   	var sel = getSelection(input);
    
    if(sel != "") {
    	// Has selection
    	insertText(input,atag+sel+etag);
    }else{
    	// No Selection
    	var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");  
    	if(insText != null) {
			insertText(input,atag + insText +etag);
		}
    }
    input.focus(); 
} 

function bbcode_tag(atag, etag) 
{ 
    var input = document.formular.text; 
   
    var sel = getSelection(input);
    
    if(sel != "") {
    	// Has selection
    	insertText(input,atag.substr(0,atag.length-1)+"]"+sel+etag);
    }else{
    	// No Selection
    	var insText = prompt("Bitte geben Sie den Linktext ein:"); 
        var url = prompt("Bitte geben Sie die URL ein:","Die URL");         
        if(insText != null) insertText(input,atag + url + ']' + insText +etag); 
    }
    
    input.focus(); 
} 

function bbcode_list() 
{ 
    var input = document.formular.text; 
    var insText = true;
    //var insText1 = '[ul]';
    var insText1 = '';
    while( insText ) {
        insText = prompt("Bitte geben Sie den zu formatierenden Text ein:",""); 
        if(insText != "" && insText != null) {
            //insText1 = insText1 + '[li]' + insText + '[/li]';
        	insText1 = insText1 + '*' + insText + '\n';
        }
    }
    //insText1 = insText1 + '[/ul]'
    input.value +=insText1;
    input.focus(); 
} 

/*
 *Smilie
 */
function insertSmilie(Smilie)
{
	
	if(!opener || opener == null || opener == self)
	{
		insertText(document.formular.text,Smilie);
		document.formular.text.focus();
	}else{
		insertText(opener.document.formular.text,Smilie);
		opener.document.formular.text.focus();
	}
}

/*
 * URL
 */
function bbcodeUrl(url) {
	if(!opener || opener == null || opener == self)
	{
		var input = document.formular.text;
	}else{
	 	var input = opener.document.formular.text;
	}
	   
    var sel = getSelection(input);
    
    if(sel != "") {
    	// Has selection
    	insertText(input,"[url="+url+"]" + sel +"[/url]");
    }else{
    	// No Selection
    	insertText(input,"[url]" + url +"[/url]"); 
    }
    input.focus();
}



var win=null;
function NewWindow(mypage,myname,w,h,pos,infocus){
if(pos=="random"){myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;}
else if((pos!='center' && pos!="random") || pos==null){myleft=0;mytop=20}
settings="width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";win=window.open(mypage,myname,settings);
win.focus();}
